package com.hxy.spring.controller;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.spring.bean.Employee;
import com.hxy.spring.bean.EmployeeExample;
import com.hxy.spring.bean.R;
import com.hxy.spring.bean.vo.EmployeeVo;
import com.hxy.spring.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Employee> list = employeeService.findAllEmp();
        model.addAttribute("list", list);
        return "employee/list";
    }
    @PostMapping("employeelist")
    @ResponseBody
    public R employeelist(@RequestBody(required = false) EmployeeVo employeeVo) throws Exception {

        //查询
        PageHelper.startPage(employeeVo.getPage(), employeeVo.getLimit());

        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        if (employeeVo.getLastName() != null && !employeeVo.getLastName().equals("")) {
            criteria.andLastNameLike("%" + employeeVo.getLastName() + "%");
        }
        //开始时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (employeeVo.getStart() != null && !employeeVo.getStart().equals("")) {
            Date parse = sdf.parse(employeeVo.getStart());
            criteria.andDateGreaterThanOrEqualTo(parse);
        }
        //结束时间
        if (employeeVo.getEnd() != null && !employeeVo.getEnd().equals("")) {
            Date parse = sdf.parse(employeeVo.getEnd());
            criteria.andDateLessThanOrEqualTo(parse);
        }
        //查询
        List<Employee> list = employeeService.findAllEmp1(employeeExample);
        //pageInfo
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        R r = new R();
        r.setCode(0);
        r.setMsg("");
        r.setCount(pageInfo.getTotal());
        r.setData(pageInfo.getList());
        return r;
    }


    @PostMapping("/insert")
    @ResponseBody
    public String insert(@RequestBody Employee employee){
        int i=employeeService.insert(employee);
        return  i>0 ? "true" : "false" ;
    }
    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestBody Employee employee){
        int i=employeeService.update(employee);
        return  i>0 ? "true" : "false" ;
    }
    @PostMapping("/del")
    @ResponseBody
    public String del(@RequestBody Integer id){
        int i=employeeService.del(id);
        return  i>0 ? "true" : "false" ;
    }

}
